<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/6/14
 * Time: 1:17 PM
 */
namespace Admin\Controller;


use Admin\Model\AdminTopicTag;
use Application\Controller\GlobalController;
use Velacolib\Utility\Utility;
use Zend\Mvc\Controller\AbstractActionController;


class TagController extends AbstractActionController
{
    private $tagTable;
    private $topictagTable;
    private $auth;

    public function onDispatch(\Zend\Mvc\MvcEvent $e)
    {
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $this->service_locator_str = 'Admin\Model\AdminTagTable';
        $this->sm = $this->getServiceLocator();
        $this->tagTable = $this->sm->get($this->service_locator_str);

        $this->service_locator_str = 'Admin\Model\AdminTopicTagTable';
        $this->sm = $this->getServiceLocator();
        $this->topictagTable = $this->sm->get($this->service_locator_str);

        $this->auth = Utility::CheckLogin($this);
        if ($this->auth!=null && $this->auth->user_type != 3 ) {
            $this->redirect()->toRoute('admin/child',array('controller'=>'login'));
        }
        if ($this->auth==null){
            $this->redirect()->toRoute('admin/child',array('controller'=>'login'));
        }
        return parent::onDispatch($e);
    }

    public function init()
    {

    }

    public function indexAction()
    {

    }

    public function addAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $val = $request->getPost('tag_name');
            $topic_id = $request->getPost('topic_id');
            $adminTag = new AdminTag();
            $adminTag->tag_name = $val;
            $tag_id = $this->tagTable->saveTag($adminTag);

            if($tag_id!=null){
                $adminTopicTag = new AdminTopicTag();
                $adminTopicTag->tag_id = $tag_id;
                $adminTopicTag->topic_id = $topic_id;
                $this->topictagTable->saveTag($adminTopicTag);

            }


        }
    }

    public function deleteAction()
    {

    }

    public function editAction()
    {


    }

    public function detailAction()
    {



    }

    public function excutedetailAction()
    {
    }

    public function topicExampleAction()
    {



    }

    public function topicexcuteAction()
    {



    }

    public function gettagbytopicAction(){
        $topic_id = $this->params()->fromQuery('topic_id');
        $val = $this->topictagTable->getTagByPostId($topic_id);
        foreach($val as $v){
            echo '<pre>';
            print_r($v);
            echo '</pre>';
        }
        die;
    }
}